package com.ycc.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ycc.biz.common.PageResult;
import com.ycc.biz.mapper.BizCategoryMapper;
import com.ycc.biz.pojo.entity.BizCategory;
import com.ycc.biz.pojo.query.BizCategoryQuery;
import com.ycc.biz.service.BizCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @ClassName: BizCategoryServiceImpl
 * @author: ycl
 * @date: 2025/03/28 16:22:58
 */
@Service
@Slf4j
public class BizCategoryServiceImpl extends ServiceImpl<BizCategoryMapper, BizCategory> implements BizCategoryService {

    @Override
    public PageResult<BizCategory> pageCategories(BizCategoryQuery query) {
        // 构建查询条件
        LambdaQueryWrapper<BizCategory> queryWrapper = new LambdaQueryWrapper<>();
        
        // 分类名称模糊查询
        if (StringUtils.hasText(query.getCategoryName())) {
            queryWrapper.like(BizCategory::getCategoryName, query.getCategoryName());
        }
        
        // 父分类ID查询
        if (query.getParentId() != null) {
            queryWrapper.eq(BizCategory::getParentId, query.getParentId());
        }
        
        // 排序（默认按创建时间降序）
        queryWrapper.orderByDesc(BizCategory::getCreateTime);
        
        // 执行分页查询
        Page<BizCategory> page = page(query.buildPage(), queryWrapper);
        
        // 转换为分页响应
        return PageResult.build(page);
    }
}